﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F920_Bao_cao_luong_nhan_vien : Form
    {
        public F920_Bao_cao_luong_nhan_vien()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private void set_define_events()
        {
            this.Load += F920_Bao_cao_luong_nhan_vien_Load;
            this.m_cmd_xem_bao_cao.Click += m_cmd_xem_bao_cao_Click;
            this.m_txt_loc_du_lieu.TextChanged += m_txt_loc_du_lieu_TextChanged;
        }

        private void load_data_to_grid()
        {
            var v_lst_luong = db.DM_LUONG
                .Where(x =>
                    (x.NGAY_THANG >= m_dtp_tu_ngay.Value
                    && x.NGAY_THANG <= m_dtp_den_ngay.Value)
                && (x.HT_NGUOI_SU_DUNG.MA_NHAN_VIEN.Contains(m_txt_loc_du_lieu.Text)
                || x.HT_NGUOI_SU_DUNG.HO_TEN_NHAN_VIEN.Contains(m_txt_loc_du_lieu.Text)
                || m_txt_loc_du_lieu.Text.Equals("")
                ))
                .ToList();
            var v_thong_ke = db.HT_NGUOI_SU_DUNG
                .OrderBy(x => x.MA_NHAN_VIEN)
                .ToList()
                .Select(x => new
                {
                    ID = x.ID,
                    MA_NHAN_VIEN = x.MA_NHAN_VIEN,
                    HO_TEN_NHAN_VIEN = x.HO_TEN_NHAN_VIEN,
                    TONG_LUONG = v_lst_luong.Where(y => y.ID_NHAN_VIEN == x.ID).Sum(l => l.LUONG + l.TIEN_THUONG - l.TIEN_PHAT)
                })
                .OrderByDescending(x => x.TONG_LUONG)
                .ToList();
            m_grv.DataSource = v_thong_ke;
            m_grv.Columns[0].HeaderText = "ID";
            m_grv.Columns[0].Width = 20;
            m_grv.Columns[0].Visible = false;
            m_grv.Columns[1].HeaderText = "Mã nhân viên";
            m_grv.Columns[1].Width = 100;
            m_grv.Columns[2].HeaderText = "Họ tên nhân viên";
            m_grv.Columns[2].Width = 200;
            m_grv.Columns[3].HeaderText = "Tổng lương";
            m_grv.Columns[3].Width = 100;
            m_grv.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion

        #region Events

        private void F920_Bao_cao_luong_nhan_vien_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xem_bao_cao_Click(object sender, EventArgs e)
        {
            load_data_to_grid();

            // Gán giá trị ban đầu cho datetime picker
            DateTime v_dat_dau_nam = new DateTime(DateTime.Now.Year, 1, 1);
            m_dtp_tu_ngay.Value = v_dat_dau_nam;
            m_dtp_den_ngay.Value = DateTime.Now;
        }

        private void m_txt_loc_du_lieu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        #endregion
    }
}
